package com.wfuhui.modules.order.api;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.wfuhui.modules.member.service.MemberService;
import com.wfuhui.modules.order.entity.OrderEntity;
import com.wfuhui.modules.order.entity.OrderRefundEntity;
import com.wfuhui.modules.order.service.OrderRefundService;
import com.wfuhui.modules.order.service.OrderService;
import com.wfuhui.modules.store.entity.StoreEntity;
import com.wfuhui.modules.store.service.StoreService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wfuhui.common.utils.NumberUtil;
import com.wfuhui.common.utils.Query;
import com.wfuhui.common.utils.R;

@RestController
@RequestMapping("/api/order")
public class ApiOrderController {
	
	@Autowired
	private OrderService orderService;
	@Autowired
	private OrderRefundService orderRefundService;
    @Autowired
	private StoreService storeService;
	
    @GetMapping("list")
    public R getOrderList(@RequestAttribute Long userId, Integer status, Integer page, Integer limit, Integer storeId){
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("orderStatus", status);
    	params.put("userId", userId);
        params.put("page", page);
        params.put("limit", limit);
        if(storeId != null){
            params.put("storeId", storeId);
        }
    	Query query = new Query(params);
    	List<OrderEntity> orderList = orderService.queryList(query);
        return R.ok().put("orderList", orderList);
    }
    
    @GetMapping("storeList")
    public R getStoreOrderList(@RequestAttribute Long userId, Integer status){
        StoreEntity store = storeService.queryByUserId(userId);
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("orderStatus", status);
    	params.put("storeId", store.getId());
    	List<OrderEntity> orderList = orderService.queryList(params);
        return R.ok().put("orderList", orderList);
    }
    
    @PostMapping("create")
    public R createOrder(@RequestAttribute Long userId, @RequestBody OrderEntity order){
    	order.setUserId(userId);
    	order.setCreateTime(new Date());
    	order.setOrderStatus(2);
    	orderService.createOrder(order);
    	return R.ok().put("orderNumber", order.getOrderNumber()).put("totalAmount", order.getTotalAmount()).put("id", order.getId());
    }
    
    @GetMapping("detail")
    public R getOrderDetail(Integer id) {
    	OrderEntity order = orderService.queryObject(id);
    	return R.ok().put("order", order);
    }
    
    @GetMapping("cancel")
    public R cancelOrder(Integer id) {
    	OrderEntity order = new OrderEntity();
    	order.setOrderStatus(-1);
    	order.setId(id);
    	orderService.update(order);
    	return R.ok();
    }
    
    @GetMapping("complete")
    public R complete(Integer id) {
    	OrderEntity order = new OrderEntity();
    	order.setOrderStatus(4);
    	order.setId(id);
    	orderService.update(order);
    	return R.ok();
    }
    
    @PostMapping("refund")
    public R refund(@RequestAttribute Long userId, @RequestBody OrderRefundEntity orderRefund) {
    	orderRefund.setCreateTime(new Date());
        orderRefund.setUserId(userId);
        orderRefund.setRefundNumber(NumberUtil.getOrderNumber());
    	orderRefundService.save(orderRefund);
    	return R.ok();
    }

}
